package io.renren.modules.project.controller;

import java.util.List;
import java.util.Map;

import io.renren.common.annotation.SysLog;
import io.renren.modules.base.entity.BaseTaskInfoEntity;
import net.sf.json.JSONArray;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.renren.modules.project.entity.PojectTaskEntity;
import io.renren.modules.project.service.PojectTaskService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;
import io.renren.common.utils.R;




/**
 * 项目任务表
 * 
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2018-10-16 15:02:36
 */
@RestController
@RequestMapping("/generator/pojecttask")
public class PojectTaskController {
	@Autowired
	private PojectTaskService pojectTaskService;
	
	/**
	 * 列表
	 */
	@RequestMapping("/list")
	@RequiresPermissions("generator:pojecttask:list")
	public R list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);

		List<PojectTaskEntity> pojectTaskList = pojectTaskService.queryList(query);
		int total = pojectTaskService.queryTotal(query);
		
		PageUtils pageUtil = new PageUtils(pojectTaskList, total, query.getLimit(), query.getPage());
		
		return R.ok().put("page", pageUtil);
	}
	
	
	/**
	 * 信息
	 */
	@RequestMapping("/info/{id}")
	@RequiresPermissions("generator:pojecttask:info")
	public R info(@PathVariable("id") String id){
		PojectTaskEntity pojectTask = pojectTaskService.queryObject(id);
		
		return R.ok().put("pojectTask", pojectTask);
	}

	/**
	 * 修改Task提交状态
	 */
	@RequestMapping("/updataTaskSubmit/{id}")
	public R updateTask(@PathVariable("id") String id){
		pojectTaskService.updataTaskSubmit(id);
		return R.ok();
	}

	/**
	 * 修改Task完工状态
	 */
	@SysLog("更改任务状态为完工")
	@RequestMapping("/updataTaskComplete/{id}/{projectId}/{order}")
	public R updataTaskComplete(@PathVariable("id") String id, @PathVariable("projectId") String projectId, @PathVariable("order") int order){
		PojectTaskEntity pojectTaskEntity = new PojectTaskEntity();
		pojectTaskEntity.setId(id);
		pojectTaskEntity.setProjectId(projectId);
		pojectTaskEntity.setOrder(order);
		pojectTaskEntity.setOrder(order);
		pojectTaskService.updataTaskComplete(pojectTaskEntity);
		return R.ok();
	}

	/**
	 * 修改Task驳回状态
	 */
	@SysLog("更改任务状态为驳回")
	@RequestMapping("/updataTaskReject/{id}/{price}/{userId}")
	public R updataTaskReject(@PathVariable("id") String id,@PathVariable("price") String price,@PathVariable("userId") Long userId){
		pojectTaskService.updataTaskReject(id,price,userId);
		return R.ok();
	}

	/**
	 * 修改Task驳回状态
	 */
	@SysLog("更改任务状态为驳回")
	@RequestMapping("/queryReject/{taskId}")
	public R queryReject(@PathVariable("taskId") String taskId){
		String src = pojectTaskService.queryReject(taskId);
		if (StringUtils.isNotBlank(src)){
			return R.ok().put("src",src);
		}
		return R.ok();
	}

	@RequestMapping("/milepost/{pojectId}")
	@RequiresPermissions("generator:pojecttask:milepost")
	public Map<String,Object> milepost(@PathVariable("pojectId") String poject){
		Map<String,Object> list = pojectTaskService.milepost(poject);
		return list;
	}

	/**
	 * 保存
	 */
	@SysLog("新增任务")
	@RequestMapping("/save")
	@RequiresPermissions("generator:pojecttask:save")
	public R save(@RequestBody PojectTaskEntity pojectTask){
		pojectTaskService.save(pojectTask);
		return R.ok();
	}
	
	/**
	 * 修改
	 */
	@SysLog("修改任务")
	@RequestMapping(value="/update")
	@RequiresPermissions("generator:pojecttask:update")
	public R update(@RequestBody PojectTaskEntity pojectTask){
		pojectTaskService.update(pojectTask);
		
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@RequestMapping("/delete")
	@RequiresPermissions("generator:pojecttask:delete")
	public R delete(@RequestBody String[] ids){
		pojectTaskService.deleteBatch(ids);
		
		return R.ok();
	}

	/**
	 * 获取待办任务
	 */
	@RequestMapping("/getDoingTaskList/{userId}/{roleIdList}")
	public R getDoingTaskList(@PathVariable("userId") String userId,@PathVariable("roleIdList") List<String> roleIdList){
		List<Map<String,Object>> taskList = pojectTaskService.getDoingTaskList(userId, roleIdList);
		return R.ok().put("doingTask",taskList);
	}

	/**
	 * 查询里程碑任务及工作项
	 */
	@RequestMapping("/getMilepostTask/{projectId}")
	public R getMilepostTask(@PathVariable("projectId") String projectId){
		List list = pojectTaskService.getMilepostTask(projectId);
		return R.ok().put("list",list);
	}

	/**
	 * 里程碑重新排序保存
	 */
	@RequestMapping("/updateOrder")
	public R updateOrder(@RequestBody List<PojectTaskEntity> pojectTaskEntities){
		pojectTaskService.updateOrder(pojectTaskEntities);
		return R.ok();
	}

	/**
	 * 里程碑删除工作项
	 */
	@RequestMapping("/deleteWorkitem/{id}")
	public R deleteWorkitem(@PathVariable("id") String id){
		pojectTaskService.deleteWorkitem(id);
		return R.ok();
	}

	/**
	 * 里程碑删除任务
	 */
	@RequestMapping("/deleteTask/{id}")
	public R deleteTask(@PathVariable("id") String id,@RequestBody List<PojectTaskEntity> pojectTaskEntities){
		pojectTaskService.deleteTask(id,pojectTaskEntities);
		return R.ok();
	}

	/**
	 * 里程碑添加查询任务
	 */
	@RequestMapping("/queryTemplateTask/{projectId}")
	public R queryTemplateTask(@PathVariable("projectId") String projectId){
		List list = pojectTaskService.queryTemplateTask(projectId);
		R r = new R();
		r.put("list",list);
		return r;
	}

	/**
	 * 里程碑里往项目里面添加任务
	 */
	@RequestMapping("/projectAddTask")
	public R projectAddTask(@RequestBody List<PojectTaskEntity> pojectTaskEntities){
		pojectTaskService.projectAddTask(pojectTaskEntities);
		return R.ok();
	}



}
